package com.quanxiaoha.weblog.admin.service.impl;

import com.quanxiaoha.weblog.admin.model.vo.image.DeleteImageReqVO;
import com.quanxiaoha.weblog.admin.model.vo.image.QueryImageListRspVO;
import com.quanxiaoha.weblog.admin.service.AdminImageService;
import com.quanxiaoha.weblog.admin.utils.MinioUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class AdminImageServiceImpl implements AdminImageService {

    @Resource
    private MinioUtil minioUtil;

    private static final String IMAGE_PREFIX = "image";

    @Override
    public List<QueryImageListRspVO> queryImageListRspVOList() {
        return minioUtil.getAllFile(IMAGE_PREFIX);
    }

    @Override
    public void deleteImage(DeleteImageReqVO req) {
        int firstSlashIndex = req.getSrc().indexOf('/');
        int secondSlashIndex = req.getSrc().indexOf('/', firstSlashIndex + 1);
        if (firstSlashIndex != -1 && secondSlashIndex != -1) {
            minioUtil.deleteFile(req.getSrc().substring(secondSlashIndex + 1));
        }
    }
}
